package com.atguigu.java;

import org.testng.annotations.Test;

import java.io.*;

/**
 * 测试FileInputStream和FileOutputStream的使用
 *
 * 结论：
 * 1.对于文本文件（.txt,.java,.c,.cpp），使用字符流处理
 * 2.对于非文本文件（.jpg,.mp3,.mp4,.avi,.doc,.ppt,...），使用字节流处理
 *
 *
 */
public class FileInputOutputStreamTest {

    //使用字节流FileInoutStream处理文本文件，可能出现乱码。
    @Test
    public void testFileInputStream(){
        FileInputStream fis = null;

        try {
            //1.造文件
            File file = new File("hello.txt");

            //2.造流
            fis = new FileInputStream(file);

            //3.读数据
            //注释：一个中文占3个字节
            byte[] buffer = new byte[5];
            int len;//记录每次存入的字节数
            while ((len = fis.read(buffer)) != -1){
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4.关闭资源
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*
    实现对图片复制的操作

     */
    @Test
    public void testFileInputOutputStream(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //
            File srcFile = new File("下午 趴在桌子的女孩4k动漫壁纸3840x2160_彼岸图网.jpg");
            File destFile = new File("下午 趴在桌子的女孩4k动漫壁纸3840x2160_彼岸图网2.jpg");

            //
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //
            byte[] b = new byte[5];
            int len;
            while ((len = fis.read(b)) != -1){
                fos.write(b,0,len);
            }
            System.out.println("复制成功！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //
            try {
                if(fis != null)
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (fos != null)
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //指定路径下文件的复制
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //
            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1){
                fos.write(b,0,len);
            }
            System.out.println("复制成功！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //
            try {
                if(fis != null)
                    fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Test
    public void testCopyFile(){

        long  start = System.currentTimeMillis();

        String srcPath = "雷电模拟器 2021-06-05 17-10-24.mp4";
        String destPath = "雷电模拟器 2021-06-05 17-10-24.1.mp4";
        copyFile(srcPath,destPath);

        long  end = System.currentTimeMillis();

        System.out.println("复制花费的时间为：" + (end - start));//9587
    }
}
